<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="5">
					<a-form-item label="专业" name="major">
						<a-select
							v-model:value="searchFormState.major"
							:options="majorOptions"
							style="width: 100%"
							placeholder="请选择"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="培养类型：" name="type">
						<a-select
							v-model:value="searchFormState.type"
							:options="typeOptions"
							style="width: 100%"
							placeholder="请选择"
							allow-clear
						>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label="版本：" name="year">
						<a-input-number v-model:value="searchFormState.year" placeholder="请输入" :min="2021" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label="学期" name="semester">
						<a-input-number v-model:value="searchFormState.semester" placeholder="请输入" min="1" max="8"/>
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="课程名称" name="courseName">
						<a-input v-model:value="searchFormState.courseName" placeholder="请输入" />
					</a-form-item>
				</a-col>
				<a-col :span="4" v-show="advanced">
					<a-form-item label="课程编码" name="courseCode">
						<a-input v-model:value="searchFormState.courseCode" placeholder="请输入课程编码" />
					</a-form-item>
				</a-col>
				<a-col :span="3" v-show="advanced">
					<a-form-item label="学分" name="credits">
						<a-input-number v-model:value="searchFormState.credits" placeholder="请输入" min="1" />
					</a-form-item>
				</a-col>
				<a-col :span="3" v-show="advanced">
					<a-form-item label="总学时" name="totalCreditHours">
						<a-input-number v-model:value="searchFormState.totalCreditHours" placeholder="请输入" min="0" />
					</a-form-item>
				</a-col>
				<a-col :span="4" v-show="advanced">
					<a-form-item label="课程类别" name="courseType">
						<a-input v-model:value="searchFormState.courseType" placeholder="请输入课程类别" />
					</a-form-item>
				</a-col>
				<a-col :span="4" v-show="advanced">
					<a-form-item label="课程性质" name="courseNature">
						<a-input v-model:value="searchFormState.courseNature" placeholder="请输入课程性质" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('eduCurriculumAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<!--导入-->
					<a-button @click="ImpExpRef.onOpen()">
						<template #icon><import-outlined /></template>
						<span>{{ $t('common.imports') }}</span>
					</a-button>
					<a-button @click="batchExtract">
						<template #icon><export-outlined /></template>
						提取课程
					</a-button>
					<a-button @click="openAssignForm()">
						<template #icon><export-outlined /></template>
						批量配课
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('eduCurriculumBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchEduCurriculum"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'major'">
					{{ $TOOL.dictTypeData('MAJOR', record.major) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('eduCurriculumEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['eduCurriculumEdit', 'eduCurriculumDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteEduCurriculum(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('eduCurriculumDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<ImpExp ref="ImpExpRef" />
	<AssignForm ref="assignFormRef" @submit="submitAssignForm" />
</template>

<script setup name="curriculum">
	import Form from './form.vue'
	import AssignForm from '../course/assignForm.vue'
	import eduCurriculumApi from '@/api/biz/eduCurriculumApi'
	import ImpExp from '../curriculum/impExp.vue'
	import { message } from 'ant-design-vue'
	import downloadUtil from '@/utils/downloadUtil'
	import tool from '@/utils/tool'

	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const selectedRowKeys = ref([])
	const formRef = ref()
	// 定义机构元素
	const treeData = ref([])
	//导入
	const ImpExpRef = ref()
	const assignFormRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '专业',
			width: '10%',
			dataIndex: 'major'
		},
		{
			title: '课程编码',
			dataIndex: 'courseCode'
		},
		{
			title: '课程名称',
			width: '50%',
			dataIndex: 'courseName'
		},
		{
			title: '学分',
			dataIndex: 'credits'
		},
		{
			title: '总学时',
			dataIndex: 'totalCreditHours'
		},
		{
			title: '学期',
			dataIndex: 'semester'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['eduCurriculumEdit', 'eduCurriculumDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	// 专业数据字典
	const majorOptions = tool.dictList('MAJOR')
	const typeOptions = tool.dictList('STUDENT_TYPE')

	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return eduCurriculumApi.eduCurriculumPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteEduCurriculum = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		eduCurriculumApi.eduCurriculumDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量提取课程
	const batchExtract = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请输入查询条件或勾选要导出的信息')
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				curriculumIds: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			extract(params)
			return
		}
	}
	// 批量提取
	const extract = (params) => {
		eduCurriculumApi.eduCurriculumExtract(params).then(() => {
			message.success('操作成功')
			table.value.clearSelected()
		})
	}
	// 批量删除
	const deleteBatchEduCurriculum = (params) => {
		eduCurriculumApi.eduCurriculumDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	// 打开配课参数窗口
	const openAssignForm = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请勾选要配课的记录')
			return
		}
		const params = {
			courseIds: selectedRowKeys.value
				.map((m) => {
					return m
				})
				.join()
		}
		assignFormRef.value.onOpen(params)
	}
	// 提交配课参数
	const submitAssignForm = (major, studentType, grade) => {
		// 处理传递的值
		console.log(major, studentType, grade)
		if (selectedRowKeys.value.length < 1) {
			message.warning('请输入查询条件或勾选要导出的信息')
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				major: major,
				studentType: studentType,
				grade: grade,
				curriculumIds: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			exportBatchCourse(params)
			return
		}
	}
	// 批量导出
	const exportBatchCourse = (params) => {
		eduCurriculumApi.eduCurriculumAssign(params).then(() => {
			message.success('操作成功')
			table.value.clearSelected()
		})
	}
</script>
